<?php
namespace Home\Controller;
use Think\Controller;
class SignController extends Controller {
	
	public function index(){
		$url = I("param.url");
        $url = htmlspecialchars_decode($url, ENT_QUOTES); 
		$appid = "wx66b1c600bbc945e7";//C('APPID');
        $secret = "16937c461a176acda9eecfb9b27e88ab";//C('SECRET');
        //token
		$access_token = S('actoken');//I('cookie.actoken/s');
		if(!$access_token) {
			$url_get='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
			$response = json_decode($this->curlGet($url_get));
			$access_token = $response->access_token;
			$expire = $response->expires_in;
//			cookie("actoken",$access_token,array('expire'=>$expire,'path'=>'/'));
			S("actoken",$access_token,array('expire'=>$expire));
//			$access_token = S('actoken');
		}
        //ticket
		$jsapi_ticket = S("jsapi");//I('cookie.jsapi/s');
		if(!$jsapi_ticket) {
			$jsapi_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";
			$jsresponse = json_decode($this->curlGet($jsapi_url));
			$jsapi_ticket = $jsresponse->ticket;
			$jsexpire =  $jsresponse->expires_in;
//			cookie("jsapi",$jsapi_ticket,array('expire'=>$jsexpire,'path'=>'/'));
			S("jsapi",$jsapi_ticket,array('expire'=>$jsexpire));
//			$jsapi_ticket = S("jsapi");
		}
        //随即字符串
        $noncestr=md5(time());//"Wm3WZYTPz0wzccnW";
        //时间戳
        $timestamp = time();
        //生成签名
        $signature = sha1("jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."&timestamp=".$timestamp."&url=".$url);
        $data['appId'] = $appid;
        $data['nonceStr'] = $noncestr;
        $data['timestamp'] = $timestamp;
        $data['signature'] = $signature;
        $data['url'] = $url;
		$data['access_token'] = $access_token;
		$data['jsapi_ticket'] = $jsapi_ticket;
		S("jsdata",json_encode($data),array('expire'=>604800));
        $this->ajaxReturn($data);
    }
	public function getPaySign(){
		$url = I("param.url");
        $url = htmlspecialchars_decode($url, ENT_QUOTES); 
		$appid = C('APPID');
        $secret = C('SECRET');
        $access_token = S("autoken");//I('cookie.autoken/s');
        //随即字符串
        $noncestr=md5(time());//"Wm3WZYTPz0wzccnW";
        	//时间戳
       	$timestamp = time();
        	//生成签名
        	$signature = sha1("accesstoken=".$access_token."&appid=".$appid."&noncestr=".$noncestr."&timestamp=".$timestamp."&url=".$url);
       	$data['appId'] = $appid;
        	$data['nonceStr'] = $noncestr;
        	$data['timeStamp'] = $timestamp;
        	$data['addrSign'] = $signature;
       	$data['url'] = $url;
		$data['access_token'] = $access_token;
		//$data['openid'] = $openid;		
		$this->ajaxReturn($data);
		
	}
	public function logError(){
		$data = I('param.data/s');
		$this->log_result(__DIR__.'/share_error',$data);
		$this->log_result(__DIR__.'/share_error',S("jsdata"));
		$this->log_result(__DIR__.'/share_error',S("jsapi"));
		$this->log_result(__DIR__.'/share_error',S("actoken"));
	}
	private function  log_result($file,$word) 
	{
	    $fp = fopen($file.'.'.date('Y-m-d').'.log',"a+");
	    flock($fp, LOCK_EX) ;
	    fwrite($fp,"执行日期：".strftime("%Y-%m-%d-%H：%M：%S",time())."\n".$word."\n\n");
	    flock($fp, LOCK_UN);
	    fclose($fp);
	}	
	private function curlGet($url){
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL,$url);
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
				curl_setopt($ch, CURLOPT_HEADER, FALSE);
				$res = curl_exec( $ch );
				curl_close( $ch);
				return $res;		
	}

	
}